package com.rules.admin.expression;

import com.gillion.grule.client.utils.BiFunction;
import com.gillion.grule.client.utils.Streams;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

public class RuleExpression implements Expression, Serializable {
   private List<Fact> facts = Lists.newArrayList();
   private Deduction deduction;
   private List<Attribute> attributes = Lists.newArrayList();
   private String ruleName;

   public List<Fact> getFacts() {
      return this.facts;
   }

   public void setFacts(List<Fact> facts) {
      this.facts = facts;
   }

   public void addFact(Fact fact) {
      this.facts.add(fact);
   }

   public void attribute(Attribute attribute) {
      this.attributes.add(attribute);
   }

   public void accept(ExpressionVisitor visitor) {
      visitor.vistor(this);
   }

   public String toString() {
      StringBuilder builder = new StringBuilder();
      builder.append("rule \"" + this.ruleName + "\"\n");
      Iterator var2 = this.attributes.iterator();

      while(var2.hasNext()) {
         Attribute attribute = (Attribute)var2.next();
         builder.append("\t" + attribute + "\n");
      }

      builder.append("\twhen \n").append("\n").append((String)Streams.of(this.facts).reduce(new BiFunction<String, Fact>() {
         public String apply(String s, Fact fact) {
            String st = StringUtils.isEmpty(s) ? "" : s;
            return st + fact + "\n";
         }
      })).append("\tthen\n").append(this.deduction).append("\n").append("end");
      return builder.toString();
   }

   public void setDeduction(Deduction deduction) {
      this.deduction = deduction;
   }

   public void setAttributes(List<Attribute> attributes) {
      this.attributes = attributes;
   }

   public void setRuleName(String ruleName) {
      this.ruleName = ruleName;
   }

   public Deduction getDeduction() {
      return this.deduction;
   }

   public List<Attribute> getAttributes() {
      return this.attributes;
   }

   public String getRuleName() {
      return this.ruleName;
   }
}
